<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="统计日期" prop="statDate">
        <el-date-picker
          v-model="queryParams.statDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:user-activity-stats:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="统计日期" align="center" prop="statDate" />
      <el-table-column label="统计类型" align="center" prop="statType" />
      <el-table-column label="总用户数" align="center" prop="totalUsers" />
      <el-table-column label="活跃用户数" align="center" prop="activeUsers" />
      <el-table-column label="新增用户数" align="center" prop="newUsers" />
      <el-table-column label="回访用户数" align="center" prop="returningUsers" />
      <el-table-column label="活跃率（%）" align="center" prop="activityRate" />
      <el-table-column label="登录用户数" align="center" prop="loginUsers" />
      <el-table-column label="总登录次数" align="center" prop="loginTimes" />
      <el-table-column label="平均登录次数" align="center" prop="avgLoginTimes" />
      <el-table-column label="管理员活跃用户数" align="center" prop="adminActiveUsers" />
      <el-table-column label="会员活跃用户数" align="center" prop="memberActiveUsers" />
      <el-table-column label="PC端用户数" align="center" prop="pcUsers" />
      <el-table-column label="移动端用户数" align="center" prop="mobileUsers" />
      <el-table-column label="Web端用户数" align="center" prop="webUsers" />
      <el-table-column label="上午活跃用户数(6-12)" align="center" prop="morningUsers" />
      <el-table-column label="下午活跃用户数(12-18)" align="center" prop="afternoonUsers" />
      <el-table-column label="晚上活跃用户数(18-24)" align="center" prop="eveningUsers" />
      <el-table-column label="深夜活跃用户数(0-6)" align="center" prop="nightUsers" />
      <el-table-column label="活跃度最高地区1" align="center" prop="topRegion1" />
      <el-table-column label="地区1用户数" align="center" prop="topRegion1Users" />
      <el-table-column label="活跃度最高地区2" align="center" prop="topRegion2" />
      <el-table-column label="地区2用户数" align="center" prop="topRegion2Users" />
      <el-table-column label="活跃度最高地区3" align="center" prop="topRegion3" />
      <el-table-column label="地区3用户数" align="center" prop="topRegion3Users" />
      <el-table-column label="连续1天活跃用户数" align="center" prop="consecutive1Day" />
      <el-table-column label="连续3天活跃用户数" align="center" prop="consecutive3Days" />
      <el-table-column label="连续7天活跃用户数" align="center" prop="consecutive7Days" />
      <el-table-column label="连续15天活跃用户数" align="center" prop="consecutive15Days" />
      <el-table-column label="连续30天活跃用户数" align="center" prop="consecutive30Days" />

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:user-activity-stats:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:user-activity-stats:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UserActivityStatsForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UserActivityStatsApi, UserActivityStatsVO } from '@/api/system/useractivitystats'
import UserActivityStatsForm from './UserActivityStatsForm.vue'

/** 用户活跃度统计 列表 */
defineOptions({ name: 'UserActivityStats' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UserActivityStatsVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  statDate: [],
  statType: undefined,
  totalUsers: undefined,
  activeUsers: undefined,
  newUsers: undefined,
  returningUsers: undefined,
  activityRate: undefined,
  loginUsers: undefined,
  loginTimes: undefined,
  avgLoginTimes: undefined,
  adminActiveUsers: undefined,
  memberActiveUsers: undefined,
  pcUsers: undefined,
  mobileUsers: undefined,
  webUsers: undefined,
  morningUsers: undefined,
  afternoonUsers: undefined,
  eveningUsers: undefined,
  nightUsers: undefined,
  topRegion1: undefined,
  topRegion1Users: undefined,
  topRegion2: undefined,
  topRegion2Users: undefined,
  topRegion3: undefined,
  topRegion3Users: undefined,
  consecutive1Day: undefined,
  consecutive3Days: undefined,
  consecutive7Days: undefined,
  consecutive15Days: undefined,
  consecutive30Days: undefined,
  calculationStatus: undefined,
  calculationStartTime: [],
  calculationEndTime: [],
  calculationDuration: undefined,
  errorMessage: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserActivityStatsApi.getUserActivityStatsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserActivityStatsApi.deleteUserActivityStats(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserActivityStatsApi.exportUserActivityStats(queryParams)
    download.excel(data, '用户活跃度统计.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
